//
//  YcView.m
//  yc_plugin_learn
//
//  Created by os on 2022/5/5.
//

#import "YcView.h"

@interface YcView ()<FlutterStreamHandler>

@property (nonatomic,copy) NSDictionary *args;
@property (nonatomic,assign) NSInteger viewId;

@property (nonatomic,strong) FlutterMethodChannel *channel;
@property (nonatomic,strong) FlutterEventSink sink;

@property (nonatomic,strong) UILabel *label;
@property (nonatomic,strong) UIButton *btn;

@end

@implementation YcView

- (instancetype)initWithFrame:(CGRect)frame viewIdentifier:(int64_t)viewId arguments:(id _Nullable)args registrar:(NSObject<FlutterPluginRegistrar>*)registrar
{
    self = [super init];
    if (self) {
        //初始值
        self.args = args;
        self.viewId = viewId;
        
        //处理flutter传过来的消息
        NSString* channelName = [NSString stringWithFormat:@"yc_plugin_learn_view_%lld", viewId];
        self.channel = [FlutterMethodChannel methodChannelWithName:channelName binaryMessenger:registrar.messenger];
        __weak __typeof__(self) weakSelf = self;
        [self.channel setMethodCallHandler:^(FlutterMethodCall * _Nonnull call, FlutterResult  _Nonnull result) {
            [weakSelf onMethodCall:call result:result];
        }];
        
        //主动给flutter发消息
        FlutterEventChannel * event = [FlutterEventChannel eventChannelWithName:[NSString stringWithFormat:@"yc_plugin_learn_eventChannel_view_%lld",viewId] binaryMessenger:registrar.messenger];
        [event setStreamHandler:self];
        
        //加载自定义控件，并赋值
        self.label = [[UILabel alloc] init];
        self.label.text = args[@"content"];
        [self.label setFont:[UIFont systemFontOfSize:12]];
        [self addSubview:self.label];
        
        self.btn = [[UIButton alloc] init];
        self.btn.backgroundColor = [UIColor yellowColor];
        [self.btn.titleLabel setFont:[UIFont systemFontOfSize:12]];
        [self.btn setTitleColor:[UIColor systemPinkColor] forState:UIControlStateNormal];
        [self.btn setTitle:@"点我给flutter发送消息,看打印" forState:UIControlStateNormal];
        [self.btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.btn];
        
    }
    return self;
}

//处理flutter传过来的消息
-(void)onMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result{
    if ([[call method] isEqualToString:@"updateText"]) {
        //获取参数
        NSDictionary *dict = call.arguments;
        NSString *content = dict[@"updateText"];
        if (content!=nil) {
            self.label.text=content;
        }
    }else{
        //其他方法事件
    }
}

- (void)layoutSubviews{
    [super layoutSubviews];
    CGSize size = self.bounds.size;
    
    self.label.frame = CGRectMake(0, 0, size.width, 30);
    self.btn.frame = CGRectMake(0, 30, size.width, 40);
    
}

//点击事件
- (void)click{
    self.sink(@{@"type":@"1"});
}


//[event setStreamHandler:instance]; FlutterStreamHandler代理方法，用于获取FlutterEventSink sink
- (FlutterError* _Nullable)onListenWithArguments:(id _Nullable)arguments
                                       eventSink:(FlutterEventSink)events{
    
    self.sink = events;
    return nil;
}
- (FlutterError* _Nullable)onCancelWithArguments:(id _Nullable)arguments{
    return nil;
}


@end
